package com.huyi.web.controller;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.huyi.common.result.APIResponse;
import com.huyi.web.service.DemoAsyncService;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by hsl on 2018/12/27.
 *
 */
@Slf4j
@RestController
@RequestMapping("/demoAsync")
public class DemoAsyncController {

	@Autowired 
    private DemoAsyncService demoAsyncService;  
  
    /** 
     * 测试异步方法调用顺序 
     */  
    @ApiOperation(value="测试异步方法调用顺序", notes="getEntityById")  
    @RequestMapping(value = "/testDemoAsync", method = RequestMethod.GET)
    //@PostMapping("/testDemoAsync")
    public JSONObject testDemoAsync() throws Exception {  
          
        long start = System.currentTimeMillis();  
  
        Future<String> task1 = demoAsyncService.doTaskOne();  
        Future<String> task2 = demoAsyncService.doTaskTwo();  
        Future<String> task3 = demoAsyncService.doTaskThree();  
  
        while(true) {  
            if(task1.isDone() && task2.isDone() && task3.isDone()) {  
                // 三个任务都调用完成，退出循环等待  
                break;  
            }  
            Thread.sleep(1000);  
        }  
  
        long end = System.currentTimeMillis();
        log.info("任务全部完成，总耗时：{}ms", end - start);  
        return APIResponse.returnSuccess();
    }  
}
